package com.bytedy.sboot.support.dto;


import lombok.Data;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;



public record RespEntity(Boolean success, String errorCode, Object data) {

    public static RespEntity ok(Object data) {
        return new RespEntity(true, null, data);
    }

    public static RespEntity fail(String errorCode) {
        return new RespEntity(false, null, errorCode);
    }

    public static RespEntity fail(AuthenticationException e) {
        var error = "登录失败";
        if (e instanceof BadCredentialsException) {
            error = "用户名或者密码输入错误，登录失败";
        } else if (e instanceof DisabledException) {
            error = "账户被禁用，登录失败";
        } else if (e instanceof CredentialsExpiredException) {
            error = "密码过期，登录失败";
        } else if (e instanceof AccountExpiredException) {
            error = "账户过期，登录失败";
        } else if (e instanceof LockedException) {
            error = "账户被锁定，登录失败";
        }
        return new RespEntity(false, error, null);
    }
}
